<script setup>
import { nextTick, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { Message } from "@arco-design/web-vue";
import arbitrateApi from "@/api/arbitrateApi";
import roleApi from "@/api/roleApi";
import schoolApi from "@/api/schoolApi";
import constants from "@/utils/constants";
const historyParams = history.state.params;

const router = useRouter();

const submitForm = ref();
const searchForm = ref();
const searchModel = reactive({
  planId: historyParams.planId,
});
const visible = ref(false);
let uploadVisible = ref(false);
const loading = ref(false);

let submitData = ref({
  planId: historyParams.planId
});
const roleList = ref([]);
const schoolList = ref([]);

let pagination = reactive({
  total: 0,
  current: 1,
  pageSize: 15,
  showPageSize: true,
  pageSizeOptions: [15, 25, 30, 50],
  showTotal: true,
  pageChange: (current) => {
    pagination.current = current;
    search(current, pagination.pageSize);
  },
  pageSizeChange(size) {
    pagination.pageSize = size;
    search(pagination.current, size);
  },
});

const columns = [
  {
    title: "学籍号",
    dataIndex: "studyCode",
    width: 180,
  },
  {
    title: "姓名",
    dataIndex: "name",
    width: 100,
  },
  {
    title: "仲裁项目",
    dataIndex: "projectName",
    width: 100,
  },
  {
    title: "申请原因",
    dataIndex: "reason",
    width: 200,
  },
  {
    title: "原成绩",
    dataIndex: "oldResult",
    width: 100,
  },
  {
    title: "新成绩",
    dataIndex: "newResult",
    width: 100,
  },
  {
    title: "审核状态",
    slotName: "state",
    width: 120,
  },
  {
    title: "审核备注",
    dataIndex: "reviewRemark",
    width: 200,
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    width: 180,
  },
  {
    title: "操作",
    slotName: "action",
    fixed: "right",
    width: 200,
  },
];
const data = ref([]);

const showAddDialog = () => {
  visible.value = true;
  submitForm.value.resetFields();
};

const editRecord = (record) => {
  visible.value = true;
  nextTick(() => {
    submitForm.value.resetFields();
    submitData.value = Object.assign({}, record);
  });
};

const deleteRecord = (record) => {
  arbitrateApi
    .delete({
      id: record.id,
    })
    .then((res) => {
      search();
    });
};

const handleSubmitSuccess = (values, e) => {
  values.schoolIds = values.schoolIdList.join(",");
  arbitrateApi.save(values).then((res) => {
    visible.value = false;
    Message.success("保存成功");
    search();
  });
};

const search = () => {
  var params = {
    pageNo: pagination.current,
    pageSize: pagination.pageSize,
    queryParam: Object.assign({}, searchModel),
  };
  loading.value = true;
  arbitrateApi.queryPage(params).then((res) => {
    data.value = res.records;
    pagination.total = res.total;
    loading.value = false;
  });
};

const refresh = () => {
  search();
};

const getNationName = (value) => {
  let nationItem = constants.nationList.find((res) => {
    return res.value == value;
  });
  return !nationItem ? "" : nationItem.label;
};

const queryRoleList = () => {
  roleApi.queryList().then(res => {
    roleList.value = res;
  })
}

const querySchoolList = () => {
  schoolApi.querySchoolList().then(res => {
    schoolList.value = res;
  })
}

const submitReview = (state) => {
  submitData.value.state = state;
  arbitrateApi.review(submitData.value).then(res => {
    Message.success("操作成功");
    search();
    visible.value = false;
  });
}

const reset = () => {
  searchForm.value.resetFields();
};

search();
queryRoleList();
querySchoolList();
</script>

<template>
  <div class="main-con">
    <a-card>
      <a-row>
        <a-col :flex="1">
          <a-form ref="searchForm" :model="searchModel" :label-col-props="{ span: 8 }" :wrapper-col-props="{ span: 16 }"
            label-align="left">
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item field="studyCode" label="学籍号">
                  <a-input v-model="searchModel.studyCode" placeholder="请输入学籍号" />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item>
                  <a-space :size="18">
                    <a-button type="primary" @click="refresh">
                      <template #icon>
                        <icon-search />
                      </template>
                      查询
                    </a-button>
                    <a-button @click="reset">
                      <template #icon>
                        <icon-refresh />
                      </template>
                      重置
                    </a-button>
                  </a-space>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
      </a-row>
    </a-card>
    <a-card style="margin-top: 20px">
      <a-table :columns="columns" row-key="id" :loading="loading" :data="data" @page-change="pagination.pageChange"
        @page-size-change="pagination.pageSizeChange" :pagination="pagination" :bordered="false">
        <template #state="{ record }">
          <a-space>
            <a-tag :color="record.state == '待审核' ? 'orange' : 'blue'">{{ record.state }}</a-tag>
          </a-space>
        </template>
        <template #action="{ record }">
          <a-space>
            <a-button v-if="record.state == '待审核'" size="small" type="text" @click="editRecord(record)">审核</a-button>
            <a-popconfirm content="确认要删除该数据吗？" @ok="deleteRecord(record)">
              <a-button v-if="record.state == '待审核'" size="small" type="text">删除</a-button>
            </a-popconfirm>
          </a-space>
        </template>
      </a-table>
    </a-card>
  </div>
  <a-modal v-model:visible="visible" title="仲裁审核" :footer="null">
    <a-form ref="submitForm" size="large" :label-col-props="{ span: 6 }" :wrapper-col-props="{ span: 18 }"
      :model="submitData">
      <a-form-item field="oldResult" label="原成绩" :rules="[{ required: true, message: '请输入原成绩' }]">
        <a-input disabled v-model.trim="submitData.oldResult" placeholder="请输入原成绩" />
      </a-form-item>
      <a-form-item field="newResult" label="新成绩" :rules="[{ required: true, message: '请输入新成绩' }]">
        <a-input v-model.trim="submitData.newResult" placeholder="请输入新成绩" />
      </a-form-item>
      <a-form-item field="reviewRemark" label="审核备注" :rules="[{ required: true, message: '请输入审核备注' }]">
        <a-textarea v-model.trim="submitData.reviewRemark" placeholder="请输入审核备注" style="height: 100px;" />
      </a-form-item>
      <a-form-item>
        <a-space>
          <a-button size="small" type="primary" @click="submitReview('审核通过')">审核通过</a-button>
          <a-button size="small" @click="submitReview('审核不通过')">审核不通过</a-button>
        </a-space>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<style scoped></style>
